﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace DataAccess
{
    public class ProductRepository: ConnectionClass
    {
        public ProductRepository() : base() { }

        //-----------------------Product Catalogue-------------------------

        public IEnumerable<Product> GetAllProducts()
        {
            return entity.Products;
        }

        public Product GetProductByID(int id)
        {
            return entity.Products.SingleOrDefault(prod => prod.ID == id);
        }

        public IEnumerable<User_Ratings> GetProductRatings(int id)
        {
            return entity.User_Ratings.Where(ur => ur.Product_Fk == id);
        }

        public IEnumerable<Comment> GetProductComments(int id)
        {
            return entity.Comments.Where(c => c.ProductID_FK == id);
        }

        //-------------------------Shopping Cart Methods-----------------------------

        public void AddToCart(ShoppingCart sc)
        {
            entity.AddToShoppingCarts(sc);
            entity.SaveChanges();
        }

        public void DeleteFromCart(ShoppingCart sc)
        {
            entity.DeleteObject(sc);
            entity.SaveChanges();
        }

        public IEnumerable<ShoppingCart> GetShoppingCartByUsername(string username)
        {
            return entity.ShoppingCarts.Where(sc => sc.Username == username);
        }

        public ShoppingCart GetShoppingCartProduct(int shoppingCartID)
        {
            return entity.ShoppingCarts.SingleOrDefault(sc => sc.ID == shoppingCartID);
        }

        //-----------------------Order Methods------------------------------------


        public void AddToOrders(Order order)
        {
            entity.AddToOrders(order);
            entity.SaveChanges();
        }

        public void AddToOrderDetails(OrderDetail od)
        {
            entity.AddToOrderDetails(od);
            entity.SaveChanges();
        }

        public void UpdateOrderDetail(OrderDetail od)
        {
            OrderDetail origin = GetOrderDetail(od.ID);
            entity.OrderDetails.Attach(origin);
            entity.OrderDetails.ApplyCurrentValues(od);
            entity.SaveChanges();
        }

        public OrderDetail GetOrderDetail(int orderDetailID)
        {
            return entity.OrderDetails.SingleOrDefault(od => od.ID == orderDetailID);
        }

        public IEnumerable<Order> GetOrder(string username)
        {
            return entity.Orders.Where(o => o.Username == username);
        }

        public IEnumerable<OrderDetail> GetAllOrdersDetails(Guid orderID)
        {
            return entity.OrderDetails.Where(od => od.OrderID_FK == orderID);
        }

        //-----------------------Rating and Comments Methods------------------------------------

        public IEnumerable<Rating> GetRatingList()
        {
            return entity.Ratings;
        }

        public void AddRating(User_Ratings ur)
        {
            entity.AddToUser_Ratings(ur);
            entity.SaveChanges();
        }

        public void AddComment(Comment c)
        {
            entity.AddToComments(c);
            entity.SaveChanges();
        }
    }
}
